{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import grammar\n",
    "with open(\"data.txt\",\"w+\") as f:\n",
    "    for i in range(10^8):\n",
    "        f.write(\" \".join(grammar.sentence())+\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "def decoration(function): #定义decoration函数\n",
    "    def inner(*args, **kwargs): #定义inner函数\n",
    "        time1=time.time()\n",
    "        function(*args, **kwargs)\n",
    "        time2=time.time()\n",
    "        print(function.__name__,' time cost: ',time2-time1)\n",
    "    return inner #返回函数inner值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "@decoration\n",
    "def OpenfileWithRead():\n",
    "    with open(\"data.txt\",\"r\") as f:\n",
    "        data=f.read()\n",
    "\n",
    "@decoration\n",
    "def OpenfileWithReadline():\n",
    "    ls=[]\n",
    "    with open(\"data.txt\",\"r\") as f:\n",
    "        for i in range(10^8):\n",
    "            ls.append(f.readline())\n",
    "    \n",
    "@decoration\n",
    "def OpenfileWithReadlines():\n",
    "    with open(\"data.txt\",\"r\") as f:\n",
    "        ls=f.readlines()\n",
    "    \n",
    "@decoration\n",
    "def OpenfileWithOpen():\n",
    "    ls=[]\n",
    "    with open(\"data.txt\",\"r\") as f:\n",
    "        for line in f:\n",
    "            ls.append(line)\n",
    "\n",
    "@decoration\n",
    "def OpenfileWithList():\n",
    "    with open(\"data.txt\",\"r\") as f:\n",
    "        ls=list(f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OpenfileWithRead  time cost:  0.0049877166748046875\n",
      "OpenfileWithReadline  time cost:  0.0009932518005371094\n",
      "OpenfileWithReadlines  time cost:  0.0\n",
      "OpenfileWithOpen  time cost:  0.0\n",
      "OpenfileWithList  time cost:  0.0010123252868652344\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    OpenfileWithRead()\n",
    "    OpenfileWithReadline()\n",
    "    OpenfileWithReadlines()\n",
    "    OpenfileWithOpen()\n",
    "    OpenfileWithList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
